package com.jml.队列.循环队列;


/**
 * @author:jml
 * @date: 2019/5/16 11:25
 * @description：
 */
public class 循环队列<E> {

    private E[] data;
    private int front, tail;
    private int size;

    public 循环队列(int capacity) {
        data = (E[])new Object[capacity+1];
        front=0;
        tail=0;
        size=0;
    }
    public 循环队列() {
        this(10);
    }

    public int getCapacity(){
        return data.length-1;
    }

    public boolean isEmpty(){
        return front==tail;
    }

    public int getSize(){
        return size;
    }

    //入队
    public void enqueue(E e){
        //判断是否满队列，满队列就扩容
        if((tail + 1) % data.length == front){
            resize(getCapacity() * 2);
        }
        data[tail] = e;
        tail = (tail + 1) % data.length;
        size ++;
    }

    private void resize(int newCapacity){

        E[] newData = (E[])new Object[newCapacity + 1];
        for(int i = 0 ; i < size ; i ++) {
            newData[i] = data[(i + front) % data.length];
        }
        data = newData;
        front = 0;
        tail = size;
    }

}
